package cn.com.bluemoon.daps.model.service.impl;


import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import cn.com.bluemoon.daps.model.mapper.DapDataModelTableFieldMapper;
import cn.com.bluemoon.daps.model.mapper.DapDataModelTablePartitionMapper;
import cn.com.bluemoon.daps.model.service.DapDataModelTablePartitionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据模型-模型中设计表-分区表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Slf4j
@Service
public class DapDataModelTablePartitionServiceImpl extends ServiceImpl<DapDataModelTablePartitionMapper, DapDataModelTablePartition> implements DapDataModelTablePartitionService {

    @Resource
    DapDataModelTablePartitionMapper dapDataModelTablePartitionMapper;

    @Resource
    DapDataModelTableFieldMapper dapDataModelTableFieldMapper;

    /**
     * 分区列表
     * @param id
     * @return
     */
    @Override
    public List<DapDataModelTablePartition> getPartitionList(String id) {

        // 分区列表
        List<DapDataModelTablePartition> partitions = dapDataModelTablePartitionMapper
                .selectList(new LambdaQueryWrapper<DapDataModelTablePartition>()
                .eq(DapDataModelTablePartition::getModelTableId, id)
                .eq(DapDataModelTablePartition::getBmStatus, BmStatus.ON.getCode()));
        return partitions;
    }

    /**
     * 查询新增的分区名是否重复
     * @param list
     * @return
     *//*
    @Override
    public List<String> isRepeat(List<DapDataModelTablePartition> list) {

        List<String> partitions = list.stream()
                .map(DapDataModelTablePartition::getPartitionFieldName)
                .collect(Collectors.toList());
        // 检查字段名是否重复
        List<DapDataModelTableField> fields = dapDataModelTableFieldMapper
                .selectList(new LambdaQueryWrapper<DapDataModelTableField>()
                .eq(DapDataModelTableField::getModelTableId, list.get(0).getModelTableId())
                .eq(DapDataModelTableField::getBmStatus, BmStatus.ON.getCode()));
        List<String> collect = fields.stream().filter(f -> partitions.contains(f.getFieldName()))
                .map(DapDataModelTableField::getFieldName).collect(Collectors.toList());
        List<String> duplicateElements = CheckUtils.getDuplicateElements(partitions);
        if(collect.isEmpty() && duplicateElements.isEmpty()){
            return collect;
        }
        return duplicateElements.isEmpty() ? collect : duplicateElements;
    }*/

    /**
     * 删除备份分区
     * @param modelId
     */
    @Override
    public void deletePartition(String modelId) {

        dapDataModelTablePartitionMapper.deletePartition(modelId);
    }

    /**
     * 新增或更新分区
     * @param partitions
     * @return
     */
    @Override
    public ResultBean<Boolean> insertPartition(List<DapDataModelTablePartition> partitions) {

        // 检查提交的对象是否有删除的分区
        List<String> ids = partitions.stream().filter(p -> StringUtils.isNotBlank(p.getId()))
                .map(DapDataModelTablePartition::getId).collect(Collectors.toList());
        // 查询库中的数据
        if(!ids.isEmpty()){
            List<DapDataModelTablePartition> partitionList = dapDataModelTablePartitionMapper.selectBatchIds(ids);
            boolean flag = partitionList.stream().noneMatch(p -> p.getBmStatus() == BmStatus.DELETE);
            if(!flag){
                return ResultBean.error("分区列表存在已删除的数据，请刷新页面");
            }
        }
        // 检查新增的数据是否和数据库重复
        List<String> names = partitions.stream().filter(p -> StringUtils.isBlank(p.getId()))
                .map(DapDataModelTablePartition::getPartitionFieldName).collect(Collectors.toList());
        if(!names.isEmpty()){
            List<DapDataModelTablePartition> list = dapDataModelTablePartitionMapper
                    .selectList(new LambdaQueryWrapper<DapDataModelTablePartition>()
                            .eq(DapDataModelTablePartition::getBmStatus, BmStatus.ON.getCode())
                            .eq(DapDataModelTablePartition::getModelTableId, partitions.get(0).getModelTableId())
                            .in(DapDataModelTablePartition::getPartitionFieldName, names));
            if(!list.isEmpty()){
                return ResultBean.error("数据库存在重复分区名："+list.toString());
            }
            // 查询字段名是否与分区名重复
            List<DapDataModelTableField> fields = dapDataModelTableFieldMapper
                    .selectList(new LambdaQueryWrapper<DapDataModelTableField>()
                    .eq(DapDataModelTableField::getBmStatus, BmStatus.ON.getCode())
                    .eq(DapDataModelTableField::getModelTableId, partitions.get(0).getModelTableId())
                    .in(DapDataModelTableField::getFieldName, names));
            if(!fields.isEmpty()){
                return ResultBean.error("分区名和字段名重复"+fields.stream().map(f -> f.getFieldName()).collect(Collectors.toList()).toString());
            }
        }
        List<DapDataModelTablePartition> collect = partitions.stream().peek(p -> p.setUpdateTime(null)).collect(Collectors.toList());
        return ResultBean.ok(this.saveOrUpdateBatch(collect));
    }

    /**
     * 获得到新增的分区信息
     * @return
     *//*
    @Override
    public List<DapDataModelTablePartition> getInsertPartition(String nowTableId, String oldTableId) {

        List<DapDataModelTablePartition> dapDataModelTablePartitions =
                dapDataModelTablePartitionMapper.selectInsertPartition(nowTableId, oldTableId);
        return dapDataModelTablePartitions;
    }*/

    /**
     * 获得到删除的表分区
     * @param nowTableId
     * @param oldTableId
     * @return
     *//*
    @Override
    public List<DapDataModelTablePartition> getDeletePartition(String nowTableId, String oldTableId) {

        List<DapDataModelTablePartition> dapDataModelTablePartitions =
                dapDataModelTablePartitionMapper.selectDeletePartition(nowTableId, oldTableId);
        return dapDataModelTablePartitions;
    }*/
}
